// pages/mer/wallet/index.js

var app = getApp(), fetchApi = require("../../../utils/httpPost.js");
const wxCharts = require("../../utils/wxcharts.js")
var potionLineChart = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    potionXLabel:[],
    potionYValue:[],
    totalPrice:0.00,
    mer_id:0,
    merList:[],
    merNames:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.mid){
      this.data.mer_id = parseInt(options.mid);
    }
    this.onHttpMerList()
  },
  
  /**
   * 获取页面数据的 http请求
   */
  onHttpMerList: function () {
    var that = this
    fetchApi.httpPost('/mer/myList',{
      keyword:'',
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
    }, function (res) {
      console.log("获取我的商家列表", res)
      if(res.data.totalcount > 0){
        that.data.pageIndex = that.data.pageIndex + 1;
        var mers = res.data.data.mers;
        if(mers.length > 0){
          for (var index in mers) {
            that.data.merList.splice(that.data.merList.length, 0, mers[index]);
          }
        }
      }
      that.data.mer_id = that.data.merList.length > 0 ?  that.data.merList[0].id : 0;
      that.setData({ 
        merList: that.data.merList,
        merNames:res.data.data.mer_names,
        mer_id:that.data.merList.length > 0 ?  that.data.merList[0].id : 0,
      });
      
      that.onHttpInit();
      that.setData({ isLoading: false, loading: true, netWork: true })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  changeMer(e){
    console.log('changeMer',e);
    var mer_id = e.detail;
    this.data.mer_id = mer_id;
    this.onHttpInit()
  },
  /**
   * 获取首页信息
   */
  onHttpInit(){
    let that = this;
    fetchApi.httpPost('/mer/walletInfo', { 
      mer_id: that.data.mer_id
    }, function (res) {
      console.log('钱包数据',res);
      if(res.data.code===0){
        that.data.potionXLabel = res.data.data.days
        that.data.potionYValue = res.data.data.totalPrices
        that.setData({
          mer_name:res.data.data.mer_name
        })
        wx.setNavigationBarTitle({
          title: res.data.data.mer_name,
        })
        that.getPotionChart();
      }
    }, function (res) { 
      that.setData({ isLoading: false, loading: true })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getPotionChart:function(){
    var windowWidth = 320;
    var that  = this
    try {
     var res = wx.getSystemInfoSync();
     windowWidth = res.windowWidth;
    } catch (e) {
     console.error('getSystemInfoSync failed!');
    }
    potionLineChart = new wxCharts({ //当月用电折线图配置
     canvasId: 'potionChart',
     type: 'line',
     categories:that.data.potionXLabel, //categories X轴
     animation: true,
     background: '#f5f5f5',
     series: [{
      name: '日期(近7日)',
      //data: yuesimulationData.data,
      data:that.data.potionYValue,
      format: function (val, name) {
       return val.toFixed(2) + '元';
      }
     }, 
    ],
     xAxis: {
      disableGrid: false,
     },
     yAxis: {
      title: '',
      format: function (val) {
       return val.toFixed(2);
      },
      // gridColor:'red',
      // max: 70,
      min: 0.00
     },
     width: windowWidth,
     height: 200,
     dataLabel: false,
     dataPointShape: true,
     extra: {
      lineStyle: 'curve'
     }
    });
  },
  touchHandler: function (e) { //当月用电触摸显示
    potionLineChart.showToolTip(e, { //showToolTip图表中展示数据详细内容
     background: '#7cb5ec',
     format: function (item, category) {
      return category + '日 ' + item.name + ':' + item.data
     }
    });
   },
})